package net.minecraft.src;
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode

import java.lang.reflect.Field;
import java.util.List;
import java.util.Random;

public class EntFetcher extends Entity {

    public EntFetcher(World world) {
        super(world);
        invIndex = -1;
        hook = this;
        retract();
        field_100007_b = -1;
        field_100006_c = -1;
        dataWatcher = -1;
        field_100005_d = 0;
        field_100004_f = false;
        field_100008_a = 0;
        field_100002_i = 0;
        setSize(0.5F, 0.5F);
    }

    public void setEntityDead() {
        super.setEntityDead();
        retract();
    }

    public static void retract() {
        if(hooked != null) {
            hooked = null;
        }

        if(hook != null) {
            hook.shooter = null;
            hook.isDead = true;
            hook = null;
        }
    }

    private boolean findItem() {
        ItemStack itemstack = shooter.inventory.getCurrentItem();

        if(itemstack != null);

        if(itemstack != null) {
            EntFetcher _tmp = this;

            if(itemstack.itemID == (type != 0 ? mod_AIManager.domrod_enhanced.shiftedIndex : mod_AIManager.domrod.shiftedIndex)) {
                invIndex = shooter.inventory.currentItem;
                return true;
            }
        }

        ItemStack aitemstack[] = shooter.inventory.mainInventory;

        for(int i = 0; i < aitemstack.length; i++) {
            ItemStack itemstack1 = aitemstack[i];

            if(itemstack1 == null) {
                continue;
            }

            EntFetcher _tmp1 = this;

            if(itemstack1.itemID == (type != 0 ? mod_AIManager.domrod_enhanced.shiftedIndex : mod_AIManager.domrod.shiftedIndex)) {
                invIndex = i;
                return true;
            }
        }

        return false;
    }

    public EntFetcher(World world, EntityLiving entityliving, int i) {
        super(world);
        invIndex = -1;

        if(hook != null) {
            hook.shooter = null;
            hook.isDead = true;
            hook = null;
        }

        EntFetcher _tmp = this;
        type = i;
        field_100007_b = -1;
        field_100006_c = -1;
        dataWatcher = -1;
        field_100005_d = 0;
        field_100004_f = false;
        field_100008_a = 0;
        field_100002_i = 0;
        setSize(0.5F, 0.5F);
        setLocationAndAngles(entityliving.posX, (entityliving.posY + 1.6200000000000001D) - (double)entityliving.height, entityliving.posZ, entityliving.rotationYaw, entityliving.rotationPitch);
        posX -= MathHelper.cos((rotationYaw / 180F) * 3.141593F) * 0.16F;
        posY -= 0.10000000149011611D;
        posZ -= MathHelper.sin((rotationYaw / 180F) * 3.141593F) * 0.16F;
        setPosition(posX, posY, posZ);
        prevRotationYaw = 0.0F;
        motionX = (double)(-MathHelper.sin((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F)) * 5.5D;
        motionZ = (double)(MathHelper.cos((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F)) * 5.5D;
        motionY = (double)(-MathHelper.sin((rotationPitch / 180F) * 3.141593F)) * 5.5D;
        newDoubleNBTList(new double[] {
                             motionX, motionY, motionZ, 5.5D, 1.0D
                         });
        shooter = (EntityPlayerSP)entityliving;

        if(!findItem()) {
            isDead = true;
            return;
        } else {
            hooked = null;
            hook = this;
            return;
        }
    }

    public void onCollideWithPlayer(EntityPlayer entityplayer) {
    }

    public void func_100001_a(double d, double d1, double d2, float f,
                              float f1) {
        float f2 = MathHelper.sqrt_double(d * d + d1 * d1 + d2 * d2);
        d /= f2;
        d1 /= f2;
        d2 /= f2;
        d *= f;
        d1 *= f;
        d2 *= f;
        motionX = d;
        motionY = d1;
        motionZ = d2;
        float f3 = MathHelper.sqrt_double(d * d + d2 * d2);
        prevRotationYaw = rotationYaw = (float)((Math.atan2(d, d2) * 180D) / 3.1415927410125728D);
        prevRotationPitch = rotationPitch = (float)((Math.atan2(d1, f3) * 180D) / 3.1415927410125728D);
        field_100003_h = 0;
    }

    private static Field getEntityLivingSpeedField(EntityLiving entityliving) {
        try {
            Field field = (EntityLiving.class).getDeclaredField("aq");
            field.setAccessible(true);
            return field;
        } catch(NoSuchFieldException nosuchfieldexception) {
            nosuchfieldexception.printStackTrace();
        }

        throw new RuntimeException("EntityLiving has no aq field! Do you use ax incompatible version of minecraft?");
    }

    private static void setEntityLivingSpeed(EntityLiving entityliving, float f) {
        Field field = getEntityLivingSpeedField(entityliving);

        try {
            field.setFloat(entityliving, f);
        } catch(IllegalAccessException illegalaccessexception) {
            illegalaccessexception.printStackTrace();
            throw new RuntimeException("how would this even happen");
        }
    }

    private static float getEntityLivingSpeed(EntityLiving entityliving) {
        Field field = getEntityLivingSpeedField(entityliving);

        try {
            return field.getFloat(entityliving);
        } catch(IllegalAccessException illegalaccessexception) {
            illegalaccessexception.printStackTrace();
        }

        throw new RuntimeException("how would this even happen");
    }

    private void murder() {
        if(hooked == null) {
            return;
        }

        String s = EntityList.getEntityString(hooked);
        int i = 0xffffff;
        String s1;

        if(s.equals("Creeper")) {
            s1 = "I'll get you next time.";
            i = 0xcc0000;
        } else {
            s1 = String.format(murder_lines[rand.nextInt(murder_lines.length)], new Object[] {
                                   s
                               });
        }

        mod_AIManager.displayMessage(s1, i);
    }

    public void onUpdate() {
        if(shooter == null || shooter.isDead) {
            retract();
            return;
        }

        super.onUpdate();

        if(field_100004_f) {
            int i = worldObj.getBlockId(field_100007_b, field_100006_c, dataWatcher);

            if(i != field_100005_d) {
                field_100004_f = false;
                motionX *= rand.nextFloat() * 1.2F;
                motionY *= rand.nextFloat() * 1.2F;
                motionZ *= rand.nextFloat() * 1.2F;
                field_100003_h = 0;
                field_100002_i = 0;
            } else {
                tick();
                return;
            }
        }

        if(hooked == null) {
            Object obj = null;
            Object obj1 = null;
            Object _tmp = obj1;
            Vec3D vec3d = Vec3D.createVector(posX, posY, posZ);
            Object _tmp1 = obj1;
            Vec3D vec3d1 = Vec3D.createVector(posX + motionX, posY + motionY, posZ + motionZ);
            MovingObjectPosition movingobjectposition = worldObj.rayTraceBlocks(vec3d, vec3d1);
            Object _tmp2 = obj1;
            vec3d = Vec3D.createVector(posX, posY, posZ);
            Object _tmp3 = obj1;
            vec3d1 = Vec3D.createVector(posX + motionX, posY + motionY, posZ + motionZ);

            if(movingobjectposition != null) {
                Object _tmp4 = obj1;
                vec3d1 = Vec3D.createVector(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
            }

            Entity entity = null;
            List list = worldObj.getEntitiesWithinAABBExcludingEntity(this, boundingBox.addCoord(motionX, motionY, motionZ).expand(1.0D, 1.0D, 1.0D));
            double d = 0.0D;

            for(int k = 0; k < list.size(); k++) {
                Entity entity1 = (Entity)list.get(k);

                if(!entity1.canBeCollidedWith() || entity1 == shooter && field_100002_i < 5) {
                    continue;
                }

                float f2 = 0.3F;
                AxisAlignedBB axisalignedbb = entity1.boundingBox.expand(f2, f2, f2);
                MovingObjectPosition movingobjectposition1 = axisalignedbb.func_1169_a(vec3d, vec3d1);

                if(movingobjectposition1 == null) {
                    continue;
                }

                double d1 = vec3d.distanceTo(movingobjectposition1.hitVec);

                if(d1 < d || d == 0.0D) {
                    entity = entity1;
                    d = d1;
                }
            }

            if(entity != null) {
                movingobjectposition = new MovingObjectPosition(entity);
            }

            if(movingobjectposition != null) {
                if(movingobjectposition.entityHit != null) {
                	//System.out.println(movingobjectposition.entityHit);
                	
                    //return ;
                
                    if(movingobjectposition.entityHit instanceof EntityLiving) {
                        if(movingobjectposition.entityHit == shooter) {
                            shooter.motionX = 0.0D;
                            shooter.motionY = 0.0D;
                            shooter.motionZ = 0.0D;
                            shooter.fallDistance = -20F;
                            retract();
                            return;
                        }

                        hooked = (EntityLiving)movingobjectposition.entityHit;
                        mod_AIManager.foundEnt(hooked, type);
                        retract();
                        worldObj.playSoundAtEntity(this, "random.drr", 1.0F, 1.2F / (rand.nextFloat() * 0.2F + 0.9F));
                    } else if(mod_AIManager.isMountable(movingobjectposition.entityHit)) {
                        mod_AIManager.giveOrder(movingobjectposition.entityHit);
                    }
                	
                } else {
                    retract();
                    return;
                }
            }
        } else if(hooked.isDead) {
            retract();
            return;
        } else {
            posX = hooked.posX;
            posY = hooked.boundingBox.minY + (double)hooked.height * 1.1000000000000001D;
            posZ = hooked.posZ;
            tick();
            return;
        }

        posX += motionX;
        posY += motionY;
        posZ += motionZ;
        float f = MathHelper.sqrt_double(motionX * motionX + motionZ * motionZ);
        rotationYaw = (float)((Math.atan2(motionX, motionZ) * 180D) / 3.1415927410125728D);

        for(rotationPitch = (float)((Math.atan2(motionY, f) * 180D) / 3.1415927410125728D); rotationPitch - prevRotationPitch < -180F; prevRotationPitch -= 360F) { }

        for(; rotationPitch - prevRotationPitch >= 180F; prevRotationPitch += 360F) { }

        for(; rotationYaw - prevRotationYaw < -180F; prevRotationYaw -= 360F) { }

        for(; rotationYaw - prevRotationYaw >= 180F; prevRotationYaw += 360F) { }

        rotationPitch = prevRotationPitch + (rotationPitch - prevRotationPitch) * 0.2F;
        rotationYaw = prevRotationYaw + (rotationYaw - prevRotationYaw) * 0.2F;

        if(handleWaterMovement()) {
            for(int j = 0; j < 4; j++) {
                float f1 = 0.25F;
                worldObj.spawnParticle("bubble", posX - motionX * (double)f1, posY - motionY * (double)f1, posZ - motionZ * (double)f1, motionX, motionY, motionZ);
            }
        }

        motionX *= 1.3D;
        motionY *= 1.3D;
        motionZ *= 1.3D;
        setPosition(posX, posY, posZ);
        tick();
    }

    public void tick() {
        label0: {
            if(shooter != null && !shooter.isDead && shooter.inventory.mainInventory[invIndex] != null) {
                EntFetcher _tmp = this;

                if(shooter.inventory.mainInventory[invIndex].itemID == (type != 0 ? mod_AIManager.domrod_enhanced.shiftedIndex : mod_AIManager.domrod.shiftedIndex)) {
                    break label0;
                }
            }

            retract();
            return;
        }
    	Entity obj = shooter;
        Entity obj1 = this;

        if(hooked != null) {
            if(hooked.isDead) {
                retract();
                return;
            }
        } else if(!field_100004_f) {
            return;
        }

        if(hooked != null) {
            if(!swap) {
                obj1 = obj;
                obj = hooked;
            } else if(gravity) {
                obj1 = hooked;
            }
        }

        double d = ((Entity) (obj1)).posX - ((Entity) (obj)).posX;
        double d1 = ((Entity) (obj1)).posY - ((Entity) (obj)).posY;
        double d2 = ((Entity) (obj1)).posZ - ((Entity) (obj)).posZ;
        double d3 = Math.sqrt(d * d + d1 * d1 + d2 * d2);
        double d4 = 0.20000000000000001D;

        if(reversed) {
            d4 *= -1D;
        }

        if(fixed) {
            if(fixeddistance == 0.0D) {
                fixeddistance = d3;
            }

            if(!fling) {
                double d5 = d4 >= 0.0D ? 1.0D : -1D;

                if(pulling) {
                    d3 = fixeddistance = Math.max(0.5D, fixeddistance - d5 * 0.33329999999999999D);
                }

                if(gravity) {
                    int i = 1;
                    Object obj2;

                    if(hooked == null) {
                        obj2 = shooter;
                        i = -1;
                    } else if(swap) {
                        obj2 = hooked;
                    } else {
                        obj2 = shooter;
                    }

                    float f = 1.0F;
                    float f1 = ((Entity)obj2).prevRotationPitch + (((Entity)obj2).rotationPitch - ((Entity)obj2).prevRotationPitch) * f;
                    float f2 = ((Entity)obj2).prevRotationYaw + (((Entity)obj2).rotationYaw - ((Entity)obj2).prevRotationYaw) * f;
                    double d6 = ((Entity) (obj1)).prevPosX + (((Entity) (obj1)).posX - ((Entity) (obj1)).prevPosX) * (double)f;
                    double d7 = (((Entity) (obj1)).prevPosY + (((Entity) (obj1)).posY - ((Entity) (obj1)).prevPosY) * (double)f + 1.6200000000000001D) - (double)((Entity) (obj1)).prevRotationYaw;
                    double d8 = ((Entity) (obj1)).prevPosZ + (((Entity) (obj1)).posZ - ((Entity) (obj1)).prevPosZ) * (double)f;
                    float f3 = MathHelper.cos(-f2 * 0.01745329F - 3.141593F);
                    float f4 = MathHelper.sin(-f2 * 0.01745329F - 3.141593F);
                    float f5 = -MathHelper.cos(-f1 * 0.01745329F);
                    float f6 = MathHelper.sin(-f1 * 0.01745329F);
                    float f7 = f4 * f5;
                    float f8 = f6;
                    float f9 = f3 * f5;
                    d = (d6 + (double)((float)i * f7) * fixeddistance) - ((Entity) (obj)).posX;
                    d1 = (d7 + (double)((float)i * f8) * fixeddistance) - ((Entity) (obj)).posY;
                    d2 = (d8 + (double)((float)i * f9) * fixeddistance) - ((Entity) (obj)).posZ;
                } else {
                    d = ((Entity) (obj1)).posX - (d * fixeddistance) / d3 - ((Entity) (obj)).posX;
                    d1 = ((Entity) (obj1)).posY - (d1 * fixeddistance) / d3 - ((Entity) (obj)).posY;
                    d2 = ((Entity) (obj1)).posZ - (d2 * fixeddistance) / d3 - ((Entity) (obj)).posZ;
                }

                d *= d5;
                d1 *= d5;
                d2 *= d5;
                d3 = fixeddistance;
            }
        } else if(!pulling && !fling) {
            return;
        }

        if(fling) {
            if(hooked != null && !swap && reversed) {
                d4 = -0.5D * d3;
            } else {
                d4 = (double)(d4 >= 0.0D ? 1 : -1) * 0.20000000000000001D * d3;
            }

            setEntityDead();
        }

        if(d4 * d1 > 0.0D && ((Entity) (obj)).motionY < 0.0D) {
            obj.motionY = 0.0D;
        }

        obj.motionX += (d4 * d) / d3;
        obj.motionY += (d4 * d1) / d3;
        obj.motionZ += (d4 * d2) / d3;
        obj.fallDistance = -20F;
        obj.onGround = false;
    }

    public void entityInit() {
    }

    public void writeEntityToNBT(NBTTagCompound nbttagcompound) {
        nbttagcompound.setShort("xTile", (short)field_100007_b);
        nbttagcompound.setShort("yTile", (short)field_100006_c);
        nbttagcompound.setShort("zTile", (short)dataWatcher);
        nbttagcompound.setByte("inTile", (byte)field_100005_d);
        nbttagcompound.setByte("shake", (byte)field_100008_a);
        nbttagcompound.setByte("inGround", (byte)(field_100004_f ? 1 : 0));
    }

    public void readEntityFromNBT(NBTTagCompound nbttagcompound) {
        field_100007_b = nbttagcompound.getShort("xTile");
        field_100006_c = nbttagcompound.getShort("yTile");
        dataWatcher = nbttagcompound.getShort("zTile");
        field_100005_d = nbttagcompound.getByte("inTile") & 0xff;
        field_100008_a = nbttagcompound.getByte("shake") & 0xff;
        field_100004_f = nbttagcompound.getByte("inGround") == 1;
    }

    public float getShadowSize() {
        return 0.0F;
    }

    public static final double speed = 5.5D;
    public static final double speedup = 1.3D;
    private int field_100007_b;
    private int field_100006_c;
    private int dataWatcher;
    private int field_100005_d;
    public boolean field_100004_f;
    public int field_100008_a;
    private int field_100003_h;
    private int field_100002_i;
    public EntityPlayerSP shooter;
    public static EntityLiving hooked;
    public static double fixeddistance;
    public int invIndex;
    public static EntFetcher hook;
    public static boolean fixed = false;
    public static boolean pulling = false;
    public static boolean reversed = false;
    public static boolean swap = false;
    public static boolean fling = false;
    public static boolean gravity = false;
    public static int type = 0;
    private static String murder_lines[] = {
        "The %s died", "You killed a %s"
    };

}
